<template>
<div>
  <!--  面包屑导航-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>商品列表</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card>
    <!--    表单输入框-->
    <el-row :gutter="30">
      <el-col :span="7">
        <div style="margin-top: 15px;">
          <el-input placeholder="请输入内容" v-model="queryInfo.query"  clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </div>
      </el-col>
      <el-col :span="6">
        <div style="margin-top: 15px;">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </div>
      </el-col>
    </el-row>
<!--    商品列表表格区域-->
    <el-table :data="goodsList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column prop="goods_price" label="价格" width="80px"></el-table-column>
      <el-table-column prop="goods_weight" label="重量" width="80px"></el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="200px">
        <template slot-scope="scope">
          {{scope.row.add_time | dateFormat}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="130px">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" type="primary"></el-button>
          <el-button @click="delgoods(scope.row)" size="mini" icon="el-icon-delete" type="danger"></el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    商品列表分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>
</div>
</template>

<script>
export default {
  name: 'goods',
  data () {
    return {
      // 查询条件
      queryInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      goodsList: [],
      // 总数据条数
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data } = await this.$axios.get('goods', { params: this.queryInfo })
      if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
      this.goodsList = data.data.goods
      this.total = data.data.total
    },
    // 每页条数发生变化时候触发
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 页码发生变化时候触发
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 删除商品
    delgoods (row) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async res => {
        const { data } = await this.$axios.delete(`goods/${row.goods_id}`)
        if (data.meta.status !== 200) return this.$message.error(data.meta.status)
        this.$message.success(data.meta.msg)
        this.getGoodsList()
      }).catch(() => {
        this.$message.success('取消删除商品成功')
      })
    },
    // 点击添加商品按钮触发函数
    addGoods () {
      this.$router.push('/home/goods/addgoods')
    }
  }
}
</script>

<style scoped>

</style>
